package datamodels

import (
	"micro-oauth/datamodels/enum"
	"micro-oauth/utils"
)

type User struct {
	MyGormModel

	PartnerId uint64   `json:"partner_id,string" gorm:"index;unique_index:_partner_id_scope_id_code_name_uc"`
	//Partner   *Partner `json:"partner"`

	ScopeId uint64 `json:"scope_id,string" gorm:"index;default:0;unique_index:_partner_id_scope_id_code_name_uc"`
	Scope   *Scope `json:"scope,omitempty"`
	Name    string `json:"name" gorm:"size:80;unique_index:_partner_id_scope_id_code_name_uc"`
	Code    string `json:"code" gorm:"size:80;unique_index:_partner_id_scope_id_code_name_uc"`
	Email   string `json:"email" gorm:"size:255"`

	Type         enum.UserType `json:"type" gorm:"index;size:50;default:'STAFF'"`
	ThirdPartyId uint64        `json:"third_party_id,string" gorm:"index;default:0"`
	Roles        []*Role       `json:"roles" gorm:"many2many:user_role_relation"`
	IsSystem     bool          `json:"is_system"`
	IsLocked     bool          `json:"is_locked"`
	PasswordHash string        `json:"_"`
	Phone        string        `json:"phone" gorm:"phone"`
	Password     string        `json:"password" gorm:"password"`

	PermissionCodes []string              `json:"permission_codes" gorm:"-"`
	PermissionInfos []*RolePermissionInfo `json:"permission_infos" gorm:"-"`
	Remark          string                `json:"remark" gorm:"type:text"`
	Status          enum.UserStatus       `json:"status" gorm:"index;size:50;default:'ACTIVE'"`
	BindStatus		enum.BindStatus		  `json:"bind_status" gorm:"size:50;default:'UNBINDED'"`
	PasswordStatus  int				  	  `json:"password_status,string"`
	LanType 		string				  `json:"lan_type" gorm:"size:10;default:'0'"`
}

func (u *User) ValidatePwd(pwd string) bool {
	return utils.CheckPwdHash(u.PasswordHash, pwd)
}

func (u *User) ResetPwd(pwd string) {
	u.PasswordHash = utils.PwdHashWithRandomSalt(pwd)
}

type UserInfo struct {
	UserId    uint64 `json:"user_id"`
	PartnerId uint64 `json:"partner_id"`
}

type UserDetailData struct {
	User        *User
	UserRawData []byte
}
